/* Copyright (c) 2004-2023 Peigen.info. All rights reserved. */

package info.peigen.hotpot.manage.core.service.processor.user.create;

import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.StrUtil;
import info.peigen.hotpot.common.core.result.ResultCode;
import info.peigen.hotpot.common.service.base.exception.BusinessException;
import info.peigen.hotpot.common.service.base.processor.Processor;
import info.peigen.hotpot.common.service.base.processor.ProcessorServiceContext;
import info.peigen.hotpot.manage.core.domain.User;
import info.peigen.hotpot.manage.core.service.processor.AbstractHotpotManageServiceProcessor;
import org.noear.solon.annotation.Component;

/**
 * <b>(UserCreateMobileOrEmailProcessor)</b>
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2024/6/12
 */
@Component
public class UserCreateMobileOrEmailProcessor extends AbstractHotpotManageServiceProcessor implements Processor {
    @Override
    public Integer level() {
        return 30;
    }

    @Override
    public void execute(ProcessorServiceContext serviceContext) {
        User domain = (User) serviceContext.getDomain();

        // 登录名必须符合手机号或邮箱规则
        if (StrUtil.isNotEmpty(domain.getLoginName())) {
            // 1. 符合Email规则
            if (Validator.isEmail(domain.getLoginName())) {
                domain.setEmail(domain.getLoginName());
            }

            // 2. 符合Mobile规则
            else if (Validator.isMobile(domain.getLoginName())) {
                domain.setMobile(domain.getLoginName());
            }

            // 3. 都不符合
            else {
                throw new BusinessException(ResultCode.FAILURE, "登录名必须符合手机号格式或Email格式");
            }
        }
    }

}
